#!/usr/bin/env bash
set -eu

test -f ~/.gitter || exit 0

GITTER=$(cat ~/.gitter)
STATUS=$1

REPO=awesomized/libmemcached
REF=$(GIT_DIR=libmemcached/.git git describe --abbrev --always)
REF_URL=https://github.com/${REPO}/commits/${REF}

BUILD_URL=${JOB_URL}
BUILD_TAG=$(uname -o 2>/dev/null || uname -s)
if expr "$BUILD_TAG" : ".*Linux" >/dev/null
then
	BUILD_TAG=$(source /etc/os-release; echo $PRETTY_NAME)
fi
BUILD_CXX=$(c++ --version | head -1)
if expr "${BUILD_CXX}" : "${BUILD_TAG}" >/dev/null
then
	BUILD_ENV="${BUILD_CXX}"
else
	BUILD_ENV="${BUILD_TAG}/${BUILD_CXX}"
fi

case "$STATUS" in
success)
	LEVEL=info
	;;
*)
	LEVEL=error
	;;
esac
MESSAGE="Sourcehut [${REPO}](${REF_URL}) (${REF}) [${STATUS}](${BUILD_URL}) (${BUILD_ENV})"

curl -sS "${GITTER}" --data-urlencode "level=${LEVEL}" --data-urlencode "message=${MESSAGE}"

